package com.wang.user.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.user.mapper.UserTokenDao;
import com.wang.user.model.UserToken;
import org.springframework.stereotype.Service;

@Service
public class UserTokenServiceImpl extends ServiceImpl<UserTokenDao, UserToken>  implements UserTokenService {
    /**
     * 检测token是否存在
     * @param token String openId
     * @return UserToken
     */
    @Override
    public UserToken getByOpenidAndType(String token) {
        LambdaQueryWrapper<UserToken> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserToken::getToken, token);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    public UserToken getTokenByUserId(Integer uid) {
        LambdaQueryWrapper<UserToken> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserToken::getUid, uid);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    public void bind(String openId, Integer uid) {
        UserToken userToken = new UserToken();
        userToken.setToken(openId);
        userToken.setUid(uid);
        save(userToken);
    }

}
